/* *****************************************************************************************************************************
 * (c) J@mBeL.net 2010-2015
 * Author: John Ambeliotis
 * Created: 24 Apr. 2010
 *
 * License:
 *  This file is part of Project jaNET.

    Project jaNET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Project jaNET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Project jaNET. If not, see <http://www.gnu.org/licenses/>. */

using System;
using System.IO;
using System.Collections.Generic;

namespace libJanet
{    
    class ApplicationSettings
    {
        internal struct ApplicationStructure
        {
            internal const string SystemCommRoot = "jaNET/System/Comm";
            internal const string SystemInstructionsRoot = "jaNET/Instructions";
            internal const string SystemEventsRoot = "jaNET/Events";
            internal const string SystemOthersRoot = "jaNET/System/Others";

            internal const string ComPortElement = "ComPort";
            internal const string ComBaudRateElement = "BaudRate";
            internal const string LocalHostElement = "localHost";
            internal const string LocalPortElement = "localPort";
            internal const string HttpHostNameElement = "Hostname";
            internal const string HttpPortElement = "httpPort";
            internal const string HttpAuthenticationElement = "Authentication";

            internal const string YahooWeatherFeed = "YahooForecastFeed";

            internal const string ComPortPath = SystemCommRoot + 
                                                "/" + ComPortElement;
            internal const string ComBaudRatePath = SystemCommRoot + 
                                                "/" + ComBaudRateElement;
            internal const string LocalHostPath = SystemCommRoot + 
                                                "/" + LocalHostElement;
            internal const string LocalPortPath = SystemCommRoot + 
                                                "/" + LocalPortElement;
            internal const string HttpHostNamePath = SystemCommRoot + 
                                                "/" + HttpHostNameElement;
            internal const string HttpPortPath = SystemCommRoot + 
                                                "/" + HttpPortElement;
            internal const string HttpAuthenticationPath = SystemCommRoot + 
                                                "/" + HttpAuthenticationElement;
            internal const string YahooWeatherFeedPath = SystemOthersRoot +
                                                "/" + YahooWeatherFeed;
        }

        internal string ComPort { get; private set; }
        internal string Baud { get; private set; }
        internal string LocalHost { get; private set; }
        internal string LocalPort { get; private set; }
        internal string HostName { get; private set; }
        internal string HttpPort { get; private set; }
        internal string Authentication { get; private set; }
        internal string YahooWeather { get; private set; }

        internal ApplicationSettings()
        {
            if (!File.Exists (Methods.getApplicationPath () + "AppConfig.xml")) {
                FileSystem.Log.Append("obj [ ApplicationSettings Constructor ]: arg [ AppConfig.xml, not found. ]");
                return;
            }
            if (Queries.Xml.AppConfigQuery(ApplicationStructure.ComPortPath).Count > 0)
                ComPort = Queries.Xml.AppConfigQuery(
                    ApplicationStructure.ComPortPath)
                    .Item(0).InnerText;
            if (Queries.Xml.AppConfigQuery(ApplicationStructure.ComBaudRatePath).Count > 0)
                Baud = Queries.Xml.AppConfigQuery(
                    ApplicationStructure.ComBaudRatePath)
                    .Item(0).InnerText;
            if (Queries.Xml.AppConfigQuery(ApplicationStructure.LocalHostPath).Count > 0)
                LocalHost = Queries.Xml.AppConfigQuery(
                    ApplicationStructure.LocalHostPath)
                    .Item(0).InnerText;
            if (Queries.Xml.AppConfigQuery(ApplicationStructure.LocalPortPath).Count > 0)
                LocalPort = Queries.Xml.AppConfigQuery(
                    ApplicationStructure.LocalPortPath)
                    .Item(0).InnerText;
            if (Queries.Xml.AppConfigQuery(ApplicationStructure.HttpHostNamePath).Count > 0)
                HostName = Queries.Xml.AppConfigQuery(
                    ApplicationStructure.HttpHostNamePath)
                    .Item(0).InnerText;
            if (Queries.Xml.AppConfigQuery(ApplicationStructure.HttpPortPath).Count > 0)
                HttpPort = Queries.Xml.AppConfigQuery(
                    ApplicationStructure.HttpPortPath)
                    .Item(0).InnerText;
            if (Queries.Xml.AppConfigQuery(ApplicationStructure.HttpAuthenticationPath).Count > 0)
                Authentication = Queries.Xml.AppConfigQuery(
                    ApplicationStructure.HttpAuthenticationPath)
                    .Item(0).InnerText;
            if (Queries.Xml.AppConfigQuery(ApplicationStructure.YahooWeatherFeedPath).Count > 0)
                YahooWeather = Queries.Xml.AppConfigQuery(
                    ApplicationStructure.YahooWeatherFeedPath)
                    .Item(0).InnerText;
        }
    }

    class FileSystem
    {
        internal class Settings
        {
            internal IList<String> LoadSettings(string fileName)
            {
                string fullPath = Methods.getApplicationPath() + fileName;

                if (File.Exists(fullPath))
                {
                    string input;
                    List<String> args = new List<String>();
                    TextReader tr = new StreamReader(fullPath);

                    while ((input = tr.ReadLine()) != null)
                    {
                        if (input != null)
                            args.Add(RijndaelSimple.Decrypt(input));
                        else { }
                    }
                    tr.Close();

                    return args;
                }
                return null;
            }

            internal string SaveSettings(string fileName, string settings)
            {
                try
                {
                    string[] args = settings.Split('\n');

                    string fullPath = Methods.getApplicationPath() + fileName;

                    if (File.Exists(fullPath))
                        File.Delete(fullPath);

                    TextWriter tw = new StreamWriter(fullPath);

                    for (int i = 0; i < args.Length; i++)
                        tw.WriteLine(RijndaelSimple.Encrypt(args[i].Trim()));

                    tw.Close();

                    return "Settings saved";
                }
                catch
                {
                    return "Fail to save settings";
                }
            }
        }

        internal class Log
        {
            // Read Log
            /*using (StreamReader r = File.OpenText("log.txt"))
            {
                DumpLog(r);
            }*/
            private static object _log_locker = new Object();

            internal static void Append(string logMessage)
            {
                lock (_log_locker)
                    using (StreamWriter w = File.AppendText(Methods.getApplicationPath() + "log.txt"))
                        Append(logMessage, w);
            }

            private static void Append(string logMessage, TextWriter w)
            {
                w.Write("\r\nLog Entry @ ");
                w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                    DateTime.Now.ToLongDateString());
                //w.WriteLine("  :");
                w.WriteLine("  :{0}", logMessage);
                w.WriteLine("--------------------------------------------------------------------------------------------------");
            }

            private static void DumpLog(StreamReader r)
            {
                string line;

                while ((line = r.ReadLine()) != null)
                    Console.WriteLine(line);
            }
        }
    }
}